package canglang.monitor.cases.action;

import canglang.monitor.cases.model.CaseFourModel;
import canglang.monitor.cases.service.CaseFourMgr;
import canglang.monitor.cases.to.SearchFourTO;
import canglang.monitor.core.base.action.BaseAction;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.properties.AppDefaultProperty;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.core.base.util.FileUtil;
import canglang.monitor.user.model.UserModel;
import com.opensymphony.xwork2.util.CreateIfNull;
import com.opensymphony.xwork2.util.Element;

import java.util.Date;
import java.util.List;
import java.io.OutputStream;

@SuppressWarnings("unchecked")
public class CaseFourAction extends BaseAction {
    private static final long serialVersionUID = -9181277844684841000L;
    
     private CaseFourModel caseFourModel;

    private CaseFourMgr caseFourMgr;

    private PaginatedListHelper helper;

    private SearchFourTO searchTO;

    public CaseFourModel getCaseFourModel() {
        return caseFourModel;
    }

    public void setCaseFourModel(CaseFourModel caseFourModel) {
        this.caseFourModel = caseFourModel;
    }

    public CaseFourMgr getCaseFourMgr() {
        return caseFourMgr;
    }

    public void setCaseFourMgr(CaseFourMgr caseFourMgr) {
        this.caseFourMgr = caseFourMgr;
    }

    public PaginatedListHelper getHelper() {
        return helper;
    }

    public void setHelper(PaginatedListHelper helper) {
        this.helper = helper;
    }

    public SearchFourTO getSearchTO() {
        return searchTO;
    }

    public void setSearchTO(SearchFourTO searchTO) {
        this.searchTO = searchTO;
    }

    @Element(value = CaseFourModel.class)
    @CreateIfNull(value = true)
    public List getResultList() {
        return helper != null ? helper.getList() : null;
    }

    public void setResultList(List alist) {
        if (helper == null)
            helper = new PaginatedListHelper();
        helper.setList(alist);
    }
    
    public String search() {
        if (searchTO == null) {
            searchTO = new SearchFourTO();
        }
        UserModel user=(UserModel)getRequest().getSession().getAttribute("user");
        if(!user.getUserRole().equalsIgnoreCase("A")){
            searchTO.setCreateId(String.valueOf(user.getDeptId()));
        }
        if (helper == null) {
            helper = new PaginatedListHelper();
            helper.setSortCriterion("CREATE_DATE");
        }
        helper = caseFourMgr.search(helper, searchTO);
        return SUCCESS;
    }

    public String delete() {
        for (int i = 0; i < helper.getList().size(); i++) {
            CaseFourModel u = (CaseFourModel) helper.getList().get(i);
            if (u != null && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("") && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("false")) {
                caseFourMgr.deleteCaseFour(Integer.parseInt(u.getSelectId()));
            }
        }
        this.saveMessage(AppDefaultProperty.getProperty("label.delete.successful"));
        return search();
    }

    public String add() {
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        caseFourModel.setCreateId(String.valueOf(user.getDeptId()));
        caseFourModel.setUpdateId(String.valueOf(user.getDeptId()));
        Date date = DateUtil.getDate();
        caseFourModel.setCreateDt(date);
        caseFourModel.setUpdateDt(date);
        caseFourModel.setUploadCnt(FileUtil.convertToByte(caseFourModel.getUpload()));
        caseFourModel.setUpload1Cnt(FileUtil.convertToByte(caseFourModel.getUpload1()));
        caseFourModel.setUpload2Cnt(FileUtil.convertToByte(caseFourModel.getUpload2()));
        caseFourModel=caseFourMgr.addCaseFour(caseFourModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.add.successful"));
        return SUCCESS;
    }

    public String view() {
        String selectId = caseFourModel.getSelectId();
        caseFourModel= caseFourMgr.getCaseFour(Integer.parseInt(selectId));
        return SUCCESS;
    }

    public String save() {
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        caseFourModel.setUpdateId(String.valueOf(user.getDeptId()));
        Date date = DateUtil.getDate();
        caseFourModel.setUpdateDt(date);
        if(StringUtil.isNullOrBlank(caseFourModel.getUploadFileName())){
            CaseFourModel c=caseFourMgr.getCaseFour(caseFourModel.getCaseFourId());
            caseFourModel.setUploadFileName(c.getUploadFileName());
            caseFourModel.setUploadCnt(c.getUploadCnt());
        }else{
            caseFourModel.setUploadCnt(FileUtil.convertToByte(caseFourModel.getUpload())); 
        }
        if (StringUtil.isNullOrBlank(caseFourModel.getUpload1FileName())) {
            CaseFourModel c = caseFourMgr.getCaseFour(caseFourModel.getCaseFourId());
            caseFourModel.setUpload1FileName(c.getUpload1FileName());
            caseFourModel.setUpload1Cnt(c.getUpload1Cnt());
        } else {
            caseFourModel.setUpload1Cnt(FileUtil.convertToByte(caseFourModel.getUpload1()));
        }
        if (StringUtil.isNullOrBlank(caseFourModel.getUpload2FileName())) {
            CaseFourModel c = caseFourMgr.getCaseFour(caseFourModel.getCaseFourId());
            caseFourModel.setUpload2FileName(c.getUpload2FileName());
            caseFourModel.setUpload2Cnt(c.getUpload2Cnt());
        } else {
            caseFourModel.setUpload2Cnt(FileUtil.convertToByte(caseFourModel.getUpload2()));
        }
        caseFourModel=caseFourMgr.saveCaseFour(caseFourModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.modify.successful"));
        return SUCCESS;
    }

     public String download() throws Exception {
        try {
            String selectId = caseFourModel.getSelectId();
            String recordType = caseFourModel.getSelectType();
            caseFourModel = caseFourMgr.getCaseFour(Integer.parseInt(selectId));
            getResponse().reset();
            if ("1".equalsIgnoreCase(recordType)) {
                getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(caseFourModel.getUpload1FileName().getBytes("gb2312"), "ISO8859-1"));
                getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
                byte[] content = caseFourModel.getUpload1Cnt();
                getResponse().setContentLength(content.length);
                OutputStream out = getResponse().getOutputStream();
                out.write(content);
                out.flush();
            }else if ("2".equalsIgnoreCase(recordType)) {
                getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(caseFourModel.getUpload2FileName().getBytes("gb2312"), "ISO8859-1"));
                getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
                byte[] content = caseFourModel.getUpload2Cnt();
                getResponse().setContentLength(content.length);
                OutputStream out = getResponse().getOutputStream();
                out.write(content);
                out.flush();
            }else if ("0".equalsIgnoreCase(recordType)) {
                getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(caseFourModel.getUploadFileName().getBytes("gb2312"), "ISO8859-1"));
                getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
                byte[] content = caseFourModel.getUploadCnt();
                getResponse().setContentLength(content.length);
                OutputStream out = getResponse().getOutputStream();
                out.write(content);
                out.flush();
            }
        } catch (Exception e) {
            System.out.println(e);
            throw e;
        }
        return NONE;
    }
}
